﻿
using System.Windows.Threading;
using System.Windows.Controls;
using System;
using System.Threading;
using System.Drawing;
using System.Windows.Media.Imaging;
using System.Windows.Media;
using System.Windows.Input;
using System.Windows;
using System.Diagnostics;


namespace WPFApp
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1
    {

        #region members
       // KinectSensorModul ksm;
        private DispatcherTimer _dispatcherMouseClickTimer = new DispatcherTimer();
        private Border _focusedBorder;
        #endregion

        #region constructor
        public Window1()
        {
            InitializeComponent();
            var ppl = new People();
            BizCardFlow1.ItemsSource = ppl.GetPersons();
            //KinectSensorModul ksm;
         //   ksm = KinectSensorModul.Instance;
          //  ksm.OnSkeletonFrameReadyEvent += new KinectSensorModul.SkeletonFrameReadyEvent(ksm_OnSkeletonFrameReadyEvent);
           // ksm.OnColorstreamReadyEvent += new KinectSensorModul.ColorStreamReadyEvent(ksm_OnColorstreamReadyEvent);
            _dispatcherMouseClickTimer.Interval = TimeSpan.FromMilliseconds(750);
            _dispatcherMouseClickTimer.Tick += new EventHandler(_dispatcherMouseClickTimer_Tick);
        }
        #endregion

        #region subscribed events
        void _dispatcherMouseClickTimer_Tick(object sender, EventArgs e)
        {

            // Simulate Mouse Click Action
            this._focusedBorder.Background = System.Windows.Media.Brushes.Green;
            Thread.Sleep(800);
            _dispatcherMouseClickTimer.Stop();

            if (this._focusedBorder.Name.Equals("borderAbort"))
            {
                this.Close();
            }
            else if (this._focusedBorder.Name.Equals("borderLeft"))
            {
                this.previousSlight();
            }

            else if (this._focusedBorder.Name.Equals("borderRight"))
            {
                this.nextSlight();
            }

            _dispatcherMouseClickTimer.Start();
                

        }
        void ksm_OnColorstreamReadyEvent(object sender, BitmapSource colorFrame)
        {

        }

        void ksm_OnSkeletonFrameReadyEvent(object sender, ImageSource skeletonFrame)
        {
            this.imageSkeleton.Source = skeletonFrame;
        }
        #endregion

        #region react 2 gui events
        private void borderIN(object sender, MouseEventArgs e)
        {
            Debug.WriteLine("Border IN");
            this._focusedBorder = ((Border)sender);
            this._focusedBorder.BorderBrush = System.Windows.Media.Brushes.Green;
            // Activate Mouse Click Timer
            this.MouseClickStart();
        }

        private void borderOUT(object sender, MouseEventArgs e)
        {
            Debug.WriteLine("Border Out");
            this._focusedBorder = ((Border)sender);
            this._focusedBorder.BorderBrush = System.Windows.Media.Brushes.Black;
            this._focusedBorder.Background = System.Windows.Media.Brushes.Black;
            this.MouseClickAbort();
        }

        private void Window_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {

        }

        private void button1_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            nextSlight();
        }

        private void button2_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            previousSlight();
        }

        private void BizCardFlow1_Loaded(object sender, RoutedEventArgs e)
        {

        }
       
        #endregion

        #region Helper Functions
        void MouseClickAbort()
        {
            this._dispatcherMouseClickTimer.Stop();
        }

        void MouseClickStart()
        {
            this._dispatcherMouseClickTimer.Start();
        }
        #endregion

        #region public slight controller functions
        public void nextSlight()
        {
            BizCardFlow1.SelectedIndex = BizCardFlow1.SelectedIndex + 1;
        }

        public void previousSlight()
        {
            if (BizCardFlow1.SelectedIndex > 0)
            {
                BizCardFlow1.SelectedIndex = BizCardFlow1.SelectedIndex - 1;
            }
        }
        #endregion


    }
}
